
###Clear R--------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation---------------------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("ggplot2", "psych","interplot","stargazer","dplyr","stringr","tidyr", "xtable")
ipak(packages)

###Load data-----------
load("Study2_data.RData")

### Appendix B.1: Sample characteristics-----------------

#Partisanship
table(data$Q31)
table(data$Q34)
633+13 #N=Democrats
634+21 #N=Republicans
646/(646+655) #percentage Democrats
655/(646+655) #percentage Republicans

#female
table(data$female)

#race
table(data$Q52)
127/(1175+127) #Latino

table(data$Q27)
129/(1302) #Black

1077/(1077+255)

#age
table(data$age)
median(data$age)

#education
table(data$education)
521/(521+261+514)

### Appendix B.3: Paritsan Social Identity Strength---------------------
dp <-with(data,data.frame(pid1,pid2,pid3,pid3.1,Q19,pid4,pid5,pid6))
dp[dp==""]     = NA
dp <- colwise(droplevels)(dp)
dp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(dp)
colnames(data)

dp <- colwise(.fun = function(x)as.numeric(factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))))(dp)
dp <- colwise(as.numeric)( dp)

rp <-with(data,data.frame(Q20,Q21,Q22,Q23,Q24,Q25,Q26.1,Q27.1))
rp[rp==""]     = NA
rp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(rp)
rp <- colwise(as.numeric)( rp)
data$reppid <- rowMeans(rp,na.rm=T)

data$reppid <- rowMeans(rp,na.rm=T)
data$huddy1 <- ifelse(!is.na(dp$pid1),dp$pid1,rp$Q20)
data$huddy2 <- ifelse(!is.na(dp$pid2),dp$pid2,rp$Q21)
data$huddy3 <- ifelse(!is.na(dp$pid3),dp$pid3,rp$Q22)
data$huddy4 <- ifelse(!is.na(dp$pid3.1),dp$pid3.1,rp$Q23)
data$huddy5 <- ifelse(!is.na(dp$Q19),dp$Q19,rp$Q24)
data$huddy6 <- ifelse(!is.na(dp$pid4),dp$pid4,rp$Q25)
data$huddy7 <- ifelse(!is.na(dp$pid5),dp$pid5,rp$Q26.1)
data$huddy8 <- ifelse(!is.na(dp$pid6),dp$pid6,rp$Q27.1)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)

huddy_latent<-'pid =~ NA*huddy1 +huddy2+huddy3+huddy4+huddy5+huddy6+huddy7+huddy8 
# fix variance of latent variable
pid ~~ 1*pid' 

fit <- cfa(huddy_latent, ordered = c("huddy1", "huddy2", "huddy3", "huddy4", "huddy5", "huddy6", "huddy7", "huddy8"), data=data)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[-c(9:66), ] 
names(p) <- c("Standardized Factor Loading", "p-value")
p<-xtable(caption = "Study 2 Farm Policy Experiment: Standardized Factor Loadings Partisan Social Identity Strength", label = "tab:cfaPID3", p)
print(p, type="latex", file="Study2_PID.tex", caption.placement="top")

psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)

summary(data$pidstrength)
mean(data$pidstrength, na.rm=T)
sd(data$pidstrength, na.rm=T)
kurtosis(data$pidstrength, na.rm=T)
skewness(data$pidstrength, na.rm=T)

#Histogram of PID strenght
ggplot(data, aes(x = zero1(pidstrength))) + geom_histogram(binwidth = 0.01)+theme_bw()+labs(x="Partisa Social Identity Strength", y="Count")
#save results
ggsave("Study2_PID_distribution.pdf",width=8,height=4)

### Appendix B.4: Cognitive Resources: Cognitive Reflection Test--------------------
mean(data$CRTall)
sd(data$CRTall)
skewness(data$CRTall)
kurtosis(data$CRTall)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7))),check.keys = T)
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7))),check.keys = T)

ggplot(data, aes(x = CRTall)) + geom_histogram(binwidth = 0.01)+theme_bw()+labs(x="CRT-score", y="Count")
ggsave("Study2_CRT7.pdf",width=6,height=2)

#Tetrachoric correlations
data_crt<-data.frame(data$CRT1,  data$CRT2, data$CRT3, data$CRT4, data$CRT5, data$CRT6, data$CRT7)
poly_values = polychoric(data_crt)
items_polychoric = poly_values$rho 
items_polychoric[lower.tri(items_polychoric)] <- NA #remove lower part of the triangle
p<-xtable(caption = "Study 2 Farm Policy Experiment: Tetrachoric Correlations Cognitive Reflection Test", label = "tab:corCRT2", items_polychoric)
names(p) <- c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7")
rownames(p) <- c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7")
print(p, type="latex", file="Study2_CRT.tex", caption.placement="top")

### Appendix B.4: Cognitive Resources: Need for Cognition--------------------
mean(data$nfc)
sd(data$nfc)
skewness(data$nfc)
kurtosis(data$nfc)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))),check.keys = T)
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))),check.keys = T)

data$nfc_1 <- factor(data$nfc_1,levels(as.factor(data$nfc_1))[c(3,5,6,4,2)])
data$nfc_2 <- factor(data$nfc_2,levels(as.factor(data$nfc_1)))
data$nfc_3 <- factor(data$nfc_3,levels(as.factor(data$nfc_1)))
data$nfc_4 <- factor(data$nfc_4,levels(as.factor(data$nfc_1)))
data$nfc_5 <- factor(data$nfc_5,levels(as.factor(data$nfc_1)))
data$nfc_6 <- factor(data$nfc_6,levels(as.factor(data$nfc_1)))
data$nfc_7 <- factor(data$nfc_7,levels(as.factor(data$nfc_1)))
data$nfc_8 <- factor(data$nfc_8,levels(as.factor(data$nfc_1)))
data$nfc_9 <- factor(data$nfc_9,levels(as.factor(data$nfc_1)))
data$nfc_10 <- factor(data$nfc_10,levels(as.factor(data$nfc_1)))
data$nfc_11 <- factor(data$nfc_11,levels(as.factor(data$nfc_1)))
data$nfc_12 <- factor(data$nfc_12,levels(as.factor(data$nfc_1)))
data$nfc_13 <- factor(data$nfc_13,levels(as.factor(data$nfc_1)))
data$nfc_14 <- factor(data$nfc_14,levels(as.factor(data$nfc_1)))
data$nfc_15 <- factor(data$nfc_15,levels(as.factor(data$nfc_1)))
data$nfc_16 <- factor(data$nfc_16,levels(as.factor(data$nfc_1)))
data$nfc_17 <- factor(data$nfc_17,levels(as.factor(data$nfc_1)))
data$nfc_18 <- factor(data$nfc_18,levels(as.factor(data$nfc_1)))


nfcvars <-(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))))


#nfcvars <-(mapply(function(x)zero1(as.numeric(x)),with(datafordems,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))))


nfc_latent<-'nfc =~ nfc_1 + nfc_2 +nfc_3+nfc_4+nfc_5+nfc_6+nfc_7+nfc_8+nfc_9+nfc_10+nfc_11+nfc_12+nfc_13+nfc_14+nfc_15+nfc_16+nfc_17+nfc_18'
fit <- cfa(nfc_latent, ordered = c("nfc_1", "nfc_2", "nfc_3", "nfc_4", "nfc_5", "nfc_6", "nfc_7", "nfc_8", "nfc_9", "nfc_10", "nfc_11", "nfc_12", "nfc_13", "nfc_14", "nfc_15", "nfc_16", "nfc_17", "nfc_18"), data=nfcvars)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[-c(19:128), ] 
p<-xtable(caption = "Study 2 Farm Policy Experiment: Standardized Factor Loadings Need for Cognition: ", label = "tab:cfaNFC3", p)
names(p) <- c("Standardized Factor Loading", "p-value")
print(p, type="latex", file="Study2_CFA.tex", caption.placement="top")

ggplot(data, aes(x = nfc)) + geom_histogram(binwidth = 0.02)+theme_bw()+labs(x="Need for Cognition", y="Count")
#save results
ggsave("Study2_NFC_distribution.pdf",width=6,height=4)


### Appendix B.4: Cognitive Resources: Political Knowledge--------------------
summary(data$pk)
sd(data$pk)
skewness(data$pk)
kurtosis(data$pk)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(pk1,pk2,pk3,pk4,pk5,pk6,pk7,pk8,pk9,pk10))),check.keys = T)
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(pk1,pk2,pk3,pk4,pk5,pk6,pk7,pk8,pk9,pk10))),check.keys = T)

#Tetrachoric correlations
data_pk<-data.frame(data$pk1_num,  data$pk2_num, data$pk3_num, data$pk4_num, data$pk5_num, data$pk6_num, data$pk7_num, data$pk8_num, data$pk9_num, data$pk10_num)
poly_values = polychoric(data_pk)
items_polychoric = poly_values$rho 
items_polychoric[lower.tri(items_polychoric)] <- NA #remove lower part of the triangle
p<-xtable(caption = "Study 2 Farm Policy Experiment: Tetrachoric Correlations Political Knowledge", label = "tab:corpk2", items_polychoric)
names(p) <- c("PK1", "PK2", "PK3", "PK4", "PK5", "PK6", "PK7", "PK8", "PK9", "PK10")
rownames(p) <- c("PK1", "PK2", "PK3", "PK4", "PK5", "PK6", "PK7", "PK8", "PK9", "PK10")
print(p, type="latex", file="Study2_PK.tex", caption.placement="top")

#histogram
ggplot(data, aes(x = pk)) + geom_histogram(binwidth = 0.01)+theme_bw()+labs(x="Political Knowlege", y="Count")
ggsave("Study2_PK.pdf",width=6,height=2)

### Appendix B.4: Cognitive Resources: Latent cognitive resources---------------------

#fit 
CrtNfC<-'cogresources  =~ NA*+NFC1_rec+ NFC2_rec+ NFC3_rec+ NFC4_rec+ NFC5_rec+ NFC6_rec+ NFC7_rec+ NFC8_rec+ NFC9_rec+ NFC10_rec+ NFC11_rec+ NFC12_rec+ NFC13_rec+ NFC14_rec+ NFC15_rec+ NFC16_rec+ NFC17_rec+ NFC18_rec+CRT1+CRT2+CRT3+CRT4+CRT5+CRT6+CRT7+pk1+pk2+pk3+pk4+pk5+pk6+pk7+pk8+pk9+pk10
# fix variance of latent variable
cogresources ~~ 1*cogresources'

#assess fit
fit<-cfa(CrtNfC,ordered=c("CRT1", "CRT2", "CRT3", "CRT4", "CRT5", "CRT6", "CRT7", "NFC1_rec", "NFC2_rec", "NFC3_rec", "NFC4_rec", "NFC5_rec", "NFC6_rec", "NFC7_rec", "NFC8_rec", "NFC9_rec", "NFC10_rec", "NFC11_rec", "NFC12_rec", "NFC13_rec", "NFC14_rec", "NFC15_rec", "NFC16_rec", "NFC17_rec", "NFC18_rec", "pk1", "pk2", "pk3", "pk4", "pk5", "pk6", "pk7", "pk8", "pk9", "pk10"),data=data)

#create latent Cognitive Resources variable based upon estimates from CFA
data$CogResources<-(lavPredict(fit, type = "lv", method="EBM",label = TRUE, optim.method = "nlminb"))


p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[-c(36:231), ] 
names(p) <- c("Standardized Factor Loading", "p-value")
p["battery"]<-"NfC"
p[19:25,3]<-"CRT"
p[26:35,3]<-"Political Knowledge"
p<-xtable(caption = "Study 2 Farm Policy Experiment: Standardized Factor Loadings for Cognitive Resources (NfC+CRT+Knowledge)", label = "tab:cog3", p)
print(p, type="latex", file="Study2_cog_cfa.tex", caption.placement="top")

psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7,nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18, pk1, pk2, pk3, pk4, pk5, pk6, pk7, pk8, pk9, pk10))),check.keys = T) 
psych::omega(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7,nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18, pk1, pk2, pk3, pk4, pk5, pk6, pk7, pk8, pk9, pk10))),check.keys = T) 

data$cogresources<-zero1(data$cogresources)
mean(data$cogresources,na.rm=T)
sd(data$cogresources,na.rm=T)
skewness(data$cogresources, na.rm=T)
kurtosis(data$cogresources, na.rm=T)

#Histogram of Cognitive resources
ggplot(data, aes(x = zero1(cogresources))) + geom_histogram(binwidth = 0.005)+theme_bw()+labs(x="Cognitive Resources", y="Count")
#save results
ggsave("Stud2_Cog_distribution.pdf",width=8,height=4)

### Appendix B.5: Randomization checks---------------------
balance_crt<-lm(CRTall~as.factor(data$condition), data=data)
balance_nfc<-lm(nfc~as.factor(data$condition), data=data)
balance_pk<-lm(pk~as.factor(data$condition), data=data)
balance_cog<-lm(cogresources~as.factor(data$condition), data=data)
balance_pid<-lm(pidstrength~as.factor(data$condition), data=data)

#Table B19 Balance checks: moderators
stargazer(balance_crt, balance_nfc, balance_pk, balance_cog, balance_pid, title="Study 2 Farm Policy Experiment: Balance check of moderators", align=TRUE, 
          covariate.labels=c("Democrats support", "Repbulicans support"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes.append = FALSE, 
          notes = "*p<0.05", 
          no.space=TRUE,out = "Study2Balance.tex",dep.var.labels =c("CRT", "NfC", "Pol Know", "Cog resources", "Partisan Identity Strength"),label="tab:Study3balance", digits=2)

balance_crt1<-lm(CRTall~as.factor(data$InParty)+ as.factor(data$OutParty), data=data)
balance_nfc1<-lm(nfc~as.factor(data$InParty)+ as.factor(data$OutParty), data=data)
balance_pk1<-lm(pk~as.factor(data$InParty)+ as.factor(data$OutParty), data=data)
balance_cog1<-lm(cogresources~as.factor(data$InParty)+ as.factor(data$OutParty), data=data)
balance_pid1<-lm(pidstrength~as.factor(data$InParty)+ as.factor(data$OutParty), data=data)

#Table B20 Party cues: Balance checks moderators per treatment condition
stargazer(balance_crt1, balance_nfc1,balance_pk1,balance_cog1, balance_pid1, title="Study 2 Farm Policy Experiment: Balance checks moderators per treatment condition", align=TRUE, 
          covariate.labels=c("In-party cue", "Out-party cue"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes.append = FALSE, 
          notes = "*p<0.05", 
          no.space=TRUE,out = "Study2_Balance3.tex",dep.var.labels =c("CRT", "NfC", "Pol Know", "Cog resources", "Party Identity Strength"),label="tab:Study3_balance", digits=2)

balanceSex1<-glm(data$female~as.factor(data$condition), data=data, family=binomial(link="logit"))
balanceAge1<-lm(data$age~as.factor(data$condition),data=data)
balanceEdu1<-polr(as.factor(data$edu)~as.factor(data$condition), data=data, Hess=TRUE)
balanceRace1<-glm(data$non_white~as.factor(data$condition),data=data, family=binomial(link="logit"))
balanceParty1<-glm(data$Republican_dummy~as.factor(data$condition),data=data, family=binomial(link="logit"))

#Table B21 Farm Policy Experiment: Balance Checks Demographics
stargazer(balanceSex1, balanceAge1, balanceEdu1, balanceRace1, balanceParty1, title="Study 2 Farm Policy Experiment: Balance Checks Demographics", align=TRUE, 
          dep.var.labels=c("Sex", "Age", "Education", "Non-white", "Partisanship"), 
          covariate.labels=c("Democrats support", "Repbulicans support"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "*p<0.05", 
          notes.append = FALSE, 
          no.space=TRUE,out = "Study2BalanceDem_condition.tex", label="tab:balanceDemstudy_condition", digits=2)

balanceSex<-glm(data$female~InParty+OutParty, data=data, family=binomial(link="logit"))
balanceAge<-lm(data$age~InParty+OutParty,data=data)
balanceEdu<-polr(as.factor(data$edu)~InParty+OutParty, data=data, Hess=TRUE)
balanceRace<-glm(data$non_white~InParty+OutParty,data=data, family=binomial(link="logit"))
balanceParty<-glm(data$Republican_dummy~InParty+OutParty,data=data, family=binomial(link="logit"))

#Table B22: Farm Policy Experiment: In-party vs. Out-party balance checks demographics
stargazer(balanceSex, balanceAge, balanceEdu, balanceRace, balanceParty, title="Study 2 Farm Policy Experiment: In-party vs. Out-party balance checks demographics", align=TRUE, 
          dep.var.labels=c("Sex", "Age", "Education", "Non-white", "Partisanship"), 
          covariate.labels=c("In-party cue", "Out-party cue"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          star.cutoffs=c(0.05), 
          notes = "*p<0.05", 
          notes.append = FALSE, 
          no.space=TRUE,out = "Study2BalanceDem.tex", label="tab:balanceDemstudy2", digits=2)

### Appendix B.6: Main effects-------------------
main<-(lm(DV1~InParty+OutParty+OutParty+pidstrength+zero1(CRTall)+age+female+ non_white +as.factor(education)+Republican_dummy,data))
main1<-(lm(DV1~InParty+OutParty+OutParty+pidstrength+nfc+age+female+ non_white +as.factor(education)+Republican_dummy,data))
main2<-(lm(DV1~InParty+OutParty+OutParty+pidstrength+pk+age+female+ non_white +as.factor(education)+Republican_dummy,data))
main3<-(lm(DV1~InParty+OutParty+OutParty+pidstrength+cogresources+age+female+ non_white +as.factor(education)+Republican_dummy,data))

stargazer(main, main1, main2, main3, title="Study 2 Farm Policy Experiment: Main Effect of Party Cues on Policy Support", align=TRUE, 
          covariate.labels=c("In-Party cue", "Out-Party cue", "PID Strength","CRT", "NfC", "Political Knowledge", "Cognitive resources", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes.append = FALSE, 
          star.cutoffs=c(0.05), 
          notes = "*p<0.05", 
          no.space=TRUE,out = "Study2_Farmmain.tex",dep.var.caption = "Policy support",dep.var.labels.include = F,label="tab:farmmain", digits=2)



### Appendix B.7: Inspection of non-linearity--------------------------

data$partyidentity_cats1<-NA
data$partyidentity_cats1[data$pidstrength<.562]=0
data$partyidentity_cats1[data$pidstrength>.562 & data$pidstrength<.781]=1
data$partyidentity_cats1[data$pidstrength>=.781]=2
data$partyidentity_cats1 <- factor(data$partyidentity_cats1,levels = c(0,1,2), label=c("low", "modest", "high"))

## CRT at 0 or -1SD
m0 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*partyidentity_cats1+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))-sd(zero1(CRTall)),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='partyidentity_cats1')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='partyidentity_cats1')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*partyidentity_cats1+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='partyidentity_cats1')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='partyidentity_cats1')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*partyidentity_cats1+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='partyidentity_cats1')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='partyidentity_cats1')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))
#remove lines that are not necessary
forplot <- forplot[-c(3, 7,11,15,19,23), ] 
forplot$fake<-rep(1:3,6)
forplot$fake <- factor(forplot$fake,levels = c("1","2","3"))

#NfC
## NfC -1SD
m0_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity_cats1+OutParty*scale(nfc,center=mean(nfc)-sd(nfc),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='partyidentity_cats1')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='partyidentity_cats1')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'

## NfC at mean
m1_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity_cats1+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='partyidentity_cats1')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='partyidentity_cats1')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'

## CRT at +1
m2_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity_cats1+ OutParty*scale(nfc,center=mean(nfc)+sd(nfc),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='partyidentity_cats1')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='partyidentity_cats1')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'

forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NFC")
forplot_nfc$CRT <- factor(forplot_nfc$CRT,levels = c("-1 SD","Mean","+1 SD"))
#remove lines that are not necessary
forplot_nfc <- forplot_nfc[-c(3, 7,11,15,19,23), ] 
forplot_nfc$fake<-rep(1:3,6)
forplot_nfc$fake <- factor(forplot_nfc$fake,levels = c("1","2","3"))

#Political Knowledge
## NfC -1SD
m0_pk <- lm(DV1~InParty*scale(pk,center=mean(pk)-sd(pk),scale=F)*partyidentity_cats1+OutParty*scale(pk,center=mean(pk)-sd(pk),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out_pk <- interplot(m0_pk,var1 = "OutParty",var2='partyidentity_cats1')
m_0_out_pk$data$CRT='-1 SD'
m_0_out_pk$data$Cue='Out'
m_0_in_pk <- interplot(m0_pk,var1 = "InParty",var2='partyidentity_cats1')
m_0_in_pk$data$CRT='-1 SD'
m_0_in_pk$data$Cue='In'

## NfC at mean
m1_pk <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*partyidentity_cats1+OutParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out_pk <- interplot(m1_pk,var1 = "OutParty",var2='partyidentity_cats1')
m_1_out_pk$data$CRT='Mean'
m_1_out_pk$data$Cue='Out'
m_1_in_pk <- interplot(m1_pk,var1 = "InParty",var2='partyidentity_cats1')
m_1_in_pk$data$CRT='Mean'
m_1_in_pk$data$Cue='In'

## PK at +1
m2_pk <- lm(DV1~InParty*scale(pk,center=mean(pk)+sd(pk),scale=F)*partyidentity_cats1+ OutParty*scale(pk,center=mean(pk)+sd(pk),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out_pk <- interplot(m2_pk,var1 = "OutParty",var2='partyidentity_cats1')
m_2_out_pk$data$CRT='+1 SD'
m_2_out_pk$data$Cue='Out'
m_2_in_pk <- interplot(m2_pk,var1 = "InParty",var2='partyidentity_cats1')
m_2_in_pk$data$CRT='+1 SD'
m_2_in_pk$data$Cue='In'

forplot_pk <- rbind(m_0_in_pk$data, m_0_out_pk$data,m_1_in_pk$data,m_1_out_pk$data,m_2_in_pk$data,m_2_out_pk$data)
forplot_pk$battery      <- c("Political\n Knowledge")
forplot_pk$CRT <- factor(forplot_pk$CRT,levels = c("-1 SD","Mean","+1 SD"))
#remove lines that are not necessary
forplot_pk <- forplot_pk[-c(3, 7,11,15,19,23), ] 
forplot_pk$fake<-rep(1:3,6)
forplot_pk$fake <- factor(forplot_pk$fake,levels = c("1","2","3"))

#Cognitive resources

## CRT at 0 or -1SD
m0 <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity_cats1+OutParty*scale(cogresources,center=mean(cogresources)-sd(cogresources),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='partyidentity_cats1')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='partyidentity_cats1')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity_cats1+OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='partyidentity_cats1')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='partyidentity_cats1')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity_cats1+ OutParty*scale(cogresources,center=mean(cogresources)+sd(cogresources),scale=F)*partyidentity_cats1+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='partyidentity_cats1')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='partyidentity_cats1')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_cog <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_cog$battery      <- c("Cognitive\n resources")
forplot_cog$CRT <- factor(forplot_cog$CRT,levels = c("-1 SD","Mean","+1 SD"))

#remove lines that are not necessary
forplot_cog <- forplot_cog[-c(3, 7,11,15,19,23), ] 
forplot_cog$fake<-rep(1:3,6)
forplot_cog$fake <- factor(forplot_cog$fake,levels = c("1","2","3"))

forplot_comb <- rbind(forplot, forplot_nfc, forplot_pk, forplot_cog)
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NFC","Political\n Knowledge", "Cognitive\n resources"))

ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of In-party cue and Out-party cue on Policy Support ")+geom_pointrange(aes(ymin=lb,ymax=ub, fill=Cue),alpha=1, position=position_dodge(width=0.2))+geom_hline(yintercept = 0,lty="dashed")+scale_colour_manual(values = c("dark green", "red")) +scale_x_discrete(labels = c("1" = "Low","2" = "Modest", "3"="Strong"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
ggsave("Farm_nonlinear.pdf",width=8,height=6)


### Appendix B.8: Results using the 3-item CRT battery----------------------

#make trait
data$CRT3item <- rowSums(with(data,data.frame(CRT1,CRT2,CRT3)),na.rm=T)
summary(zero1(data$CRT3item))
sd(zero1(data$CRT3item))

#Cor
cor.test(data$CRT3item, data$CRTall)

m0 <- lm(DV1~InParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item),na.rm=T)-sd(zero1(CRT3item),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item),na.rm=T)-sd(zero1(CRT3item),na.rm=T),scale=F)*pidstrength +age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(DV1~InParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item),na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item))+sd(zero1(CRT3item)),scale=F)*pidstrength+ OutParty*scale(zero1(CRT3item),center=mean(zero1(CRT3item))+sd(zero1(CRT3item)),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'
summary(m2)
forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

farm_crt3<-ggplot(forplot,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(.~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
ggsave(farm_crt3, file="study2_crt3.pdf",width=8,height=6)


### Appendix F: Tradiational Partisan Identity Strength Measure---------------
cor.test(data$tradition_pid_strength,data$pidstrength)

## CRT at 0 or -1SD
m0 <- lm(DV1~InParty*scale(CRTall,center=0,scale=F)*tradition_pid_strength+OutParty*scale(CRTall,center=0,scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='tradition_pid_strength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='tradition_pid_strength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'

## CRT at mean
m1 <- lm(DV1~InParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*tradition_pid_strength+OutParty*scale(CRTall,center=mean(CRTall,na.rm=T),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='tradition_pid_strength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='tradition_pid_strength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*tradition_pid_strength+ OutParty*scale(CRTall,center=mean(CRTall)+sd(CRTall),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_out <- interplot(m2,var1 = "OutParty",var2='tradition_pid_strength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='tradition_pid_strength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'
forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

## NFC -1SD
m0_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*tradition_pid_strength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='tradition_pid_strength')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='tradition_pid_strength')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

## NFC at mean
m1_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*tradition_pid_strength+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='tradition_pid_strength')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='tradition_pid_strength')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## CRT at +1
m2_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*tradition_pid_strength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='tradition_pid_strength')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='tradition_pid_strength')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'
forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NFC")

#Political Knowledge
## -1SD
m0_know <- lm(DV1~InParty*scale(cogresources,center=mean(data$pk)-sd(data$pk),scale=F)*tradition_pid_strength+ OutParty*scale(pk,center=mean(data$pk)-sd(data$pk),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m0_nfc)
m_0_in_know <- interplot(m0_know,var1 = "InParty",var2='tradition_pid_strength')
m_0_in_know$data$CRT='-1 SD'
m_0_in_know$data$Cue='In'
m_0_out_know <- interplot(m0_know,var1 = "OutParty",var2='tradition_pid_strength')
m_0_out_know$data$CRT='-1 SD'
m_0_out_know$data$Cue='Out'

## NFC at mean
m1_know <- lm(DV1~InParty*scale(pk,center=mean(pk),scale=F)*tradition_pid_strength+OutParty*scale(pk,center=mean(pk),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_in_know <- interplot(m1_know,var1 = "InParty",var2='tradition_pid_strength')
m_1_in_know$data$CRT='Mean'
m_1_in_know$data$Cue='In'
m_1_out_know <- interplot(m1_know,var1 = "OutParty",var2='tradition_pid_strength')
m_1_out_know$data$CRT='Mean'
m_1_out_know$data$Cue='Out'

## Know at +1
m2_know <- lm(DV1~InParty*scale(pk,center=mean(data$pk)+sd(data$pk),scale=F)*tradition_pid_strength+ OutParty*scale(pk,center=mean(data$pk)+sd(data$pk),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_in_know  <- interplot(m2_know,var1 = "InParty",var2='tradition_pid_strength')
m_2_in_know $data$CRT='+1 SD'
m_2_in_know $data$Cue='In'
m_2_out_know  <- interplot(m2_know,var1 = "OutParty",var2='tradition_pid_strength')
m_2_out_know $data$CRT='+1 SD'
m_2_out_know $data$Cue='Out'

forplot_know <- rbind(m_0_in_know$data, m_0_out_know$data,m_1_in_know$data,m_1_out_know$data,m_2_in_know$data,m_2_out_know$data)
forplot_know$battery      <- c("Political\n knowledge")

#Cognitive resources
## Knowledge -1SD
m0_know <- lm(DV1~InParty*scale(cogresources,center=mean(data$cogresources)-sd(data$cogresources),scale=F)*tradition_pid_strength+ OutParty*scale(cogresources,center=mean(data$cogresources)-sd(data$cogresources),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_in_know <- interplot(m0_know,var1 = "InParty",var2='tradition_pid_strength')
m_0_in_know$data$CRT='-1 SD'
m_0_in_know$data$Cue='In'
m_0_out_know <- interplot(m0_know,var1 = "OutParty",var2='tradition_pid_strength')
m_0_out_know$data$CRT='-1 SD'
m_0_out_know$data$Cue='Out'

## NFC at mean
m1_know <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources),scale=F)*tradition_pid_strength+OutParty*scale(cogresources,center=mean(cogresources),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_in_know <- interplot(m1_know,var1 = "InParty",var2='tradition_pid_strength')
m_1_in_know$data$CRT='Mean'
m_1_in_know$data$Cue='In'
m_1_out_know <- interplot(m1_know,var1 = "OutParty",var2='tradition_pid_strength')
m_1_out_know$data$CRT='Mean'
m_1_out_know$data$Cue='Out'

## Know at +1
m2_know <- lm(DV1~InParty*scale(cogresources,center=mean(data$cogresources)+sd(data$cogresources),scale=F)*tradition_pid_strength+ OutParty*scale(cogresources,center=mean(data$cogresources)+sd(data$cogresources),scale=F)*tradition_pid_strength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_in_know  <- interplot(m2_know,var1 = "InParty",var2='tradition_pid_strength')
m_2_in_know $data$CRT='+1 SD'
m_2_in_know $data$Cue='In'
m_2_out_know  <- interplot(m2_know,var1 = "OutParty",var2='tradition_pid_strength')
m_2_out_know $data$CRT='+1 SD'
m_2_out_know $data$Cue='Out'

forplot_cog <- rbind(m_0_in_know$data, m_0_out_know$data,m_1_in_know$data,m_1_out_know$data,m_2_in_know$data,m_2_out_know$data)
forplot_cog$battery      <- c("Cognitive\n resources")

#Combine plots
forplot_comb <- rbind(forplot, forplot_nfc, forplot_know, forplot_cog)
forplot_comb$fake <- factor(forplot_comb$fake,levels = c("0","0.5","1"))
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NFC","Political\n knowledge", "Cognitive\n resources"))

#make plot
ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+facet_grid(battery~CRT)+xlab("Party Identity Strength")+theme_bw()+ylab("Marginal effect of In-party cue and Out-party cue on Policy Support ")+geom_pointrange(aes(ymin=lb,ymax=ub, fill=Cue),alpha=1, position=position_dodge(width=0.2))+geom_hline(yintercept = 0,lty="dashed")+scale_colour_manual(values = c("dark green", "red"))+scale_x_discrete(labels = c("0" = "Not","0.5" = "Weak", "1"="Strong"))+ theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
#save results
ggsave("Study2_Farm_pidstrenght.pdf",width=8,height=6)



### Appendix G: Partisanship and policy preferences--------------------

appendix_g<-list()
summary(appendix_g[[1]]<-lm(DV1~partisanship_ind,subset(data, cues==1)))
summary(appendix_g[[2]]<-lm(DV1~Republican_dummy,subset(data, cues==1)))

402+231+235+396
table(data$Republican_dummy, data$partisanship_ind)
summary(data$partisanship_ind)
table(data$cues)
396


stargazer(appendix_g[[1]], appendix_g[[2]], title="Study 2 Farm Policy: Partisanship and policy preferences in the Control Condition (no-Cues", align=TRUE,  notes.append = FALSE, covariate.labels=c("Partisanship (6-point)", "Republican (Ref. Democrat)"), star.cutoffs=c(0.05), omit.stat=c("LL","ser","f", "adj.rsq"), notes = "*p<0.05", column.sep.width = "1pt", no.space=TRUE,  out = "Study2_FarmPartisan.tex",dep.var.caption = "Policy support", dep.var.labels.include = FALSE,label="tab:partisan3", digits=2)

### Appendix: based upon footnote - models without covariates----------------

twoway_crt<-(lm(DV1~InParty+OutParty+pidstrength+zero1(CRTall)+InParty*pidstrength+OutParty*pidstrength+InParty*zero1(CRTall)+OutParty*zero1(CRTall)+pidstrength*zero1(CRTall),data))
twoway_nfc<-(lm(DV1~InParty+OutParty+pidstrength+nfc+InParty*pidstrength+OutParty*pidstrength+InParty*nfc+OutParty*nfc+pidstrength*nfc,data))
twoway_pk<-(lm(DV1~InParty+OutParty+pidstrength+pk+InParty*pidstrength+OutParty*pidstrength+InParty*pk+OutParty*pk+pidstrength*pk,data))
twoway_cog<-(lm(DV1~InParty+OutParty+pidstrength+cogresources+InParty*pidstrength+OutParty*pidstrength+InParty*cogresources+OutParty*cogresources+pidstrength*cogresources,data))

#replace names 2 way model
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc"] <- "zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "InParty:nfc"] <- "InParty:zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "OutParty:nfc"] <- "OutParty:zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "pidstrength:nfc"] <- "pidstrength:zero1(CRTall)"

names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "pk"] <- "zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "InParty:pk"] <- "InParty:zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "OutParty:pk"] <- "OutParty:zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "pidstrength:pk"] <- "pidstrength:zero1(CRTall)"

names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources"] <- "zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "InParty:cogresources"] <- "InParty:zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "OutParty:cogresources"] <- "OutParty:zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "pidstrength:cogresources"] <- "pidstrength:zero1(CRTall)"

#3way interactions
crt_full<-(lm(DV1~InParty+OutParty+pidstrength+zero1(CRTall)+InParty*pidstrength+OutParty*pidstrength+InParty*zero1(CRTall)+OutParty*zero1(CRTall)+pidstrength*zero1(CRTall)+InParty*pidstrength*zero1(CRTall) + OutParty*pidstrength*zero1(CRTall),data))
nfc_full<-(lm(DV1~InParty+OutParty+pidstrength+nfc+InParty*pidstrength+OutParty*pidstrength+InParty*nfc+OutParty*nfc+pidstrength*nfc+InParty*pidstrength*nfc + OutParty*pidstrength*nfc,data))
pk_full<-(lm(DV1~InParty+OutParty+pidstrength+pk+InParty*pidstrength+OutParty*pidstrength+InParty*pk+OutParty*pk+pidstrength*pk+InParty*pidstrength*pk + OutParty*pidstrength*pk,data))
cog_full<-(lm(DV1~InParty+OutParty+pidstrength+cogresources+InParty*pidstrength+OutParty*pidstrength+InParty*cogresources+OutParty*cogresources+pidstrength*cogresources+InParty*pidstrength*cogresources + OutParty*pidstrength*cogresources,data))

names(nfc_full$coefficients)[names(nfc_full$coefficients) == "nfc"] <- "zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "InParty:nfc"] <- "InParty:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "OutParty:nfc"] <- "OutParty:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "pidstrength:nfc"] <- "pidstrength:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "InParty:pidstrength:nfc"] <- "InParty:pidstrength:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "OutParty:pidstrength:nfc"] <- "OutParty:pidstrength:zero1(CRTall)"

names(pk_full$coefficients)[names(pk_full$coefficients) == "pk"] <- "zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "InParty:pk"] <- "InParty:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "OutParty:pk"] <- "OutParty:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "pidstrength:pk"] <- "pidstrength:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "InParty:pidstrength:pk"] <- "InParty:pidstrength:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "OutParty:pidstrength:pk"] <- "OutParty:pidstrength:zero1(CRTall)"

names(cog_full$coefficients)[names(cog_full$coefficients) == "cogresources"] <- "zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "InParty:cogresources"] <- "InParty:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "OutParty:cogresources"] <- "OutParty:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "pidstrength:cogresources"] <- "pidstrength:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "InParty:pidstrength:cogresources"] <- "InParty:pidstrength:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "OutParty:pidstrength:cogresources"] <- "OutParty:pidstrength:zero1(CRTall)"


stargazer(twoway_crt, crt_full,twoway_nfc, nfc_full, twoway_pk, pk_full, twoway_crt, cog_full, title="Farm Subsidy Experiment: Policy support, party cues, reflection and social identity strength", align=TRUE, covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","Cognitive resource", "In-party * PSID", "Out-party * PSID", "In-party * Cognitive", "Out-party * Cognitive", "PSID * Cognitive", "In-party * PSID * Cognitive", "Out-party * PSID * Cognitive"), omit.stat=c("LL","ser","f", "adj.rsq"), notes.append = FALSE, star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"), notes = "+p<.1; *p<0.05", column.sep.width = "1pt",  no.space=TRUE,font.size="tiny" ,dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Knowledge", "Cog resources"), column.separate = c(2, 2, 2,2), dep.var.labels.include = F, digits=2)

## CRT at 0 or -1SD
m0 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T)-sd(zero1(CRTall),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T)-sd(zero1(CRTall),na.rm=T),scale=F)*pidstrength,data)
summary(m0)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'
summary(m0)
## CRT at mean
m1 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength,data)
summary(m1)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength,data)
summary(m2)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#NFC
## NFC -1SD
m0_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*pidstrength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*pidstrength,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='pidstrength')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='pidstrength')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

## NFC at mean
m1_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*pidstrength+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*pidstrength,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='pidstrength')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='pidstrength')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## NFC at +1
m2_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*pidstrength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*pidstrength,data)
summary(m2_nfc)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='pidstrength')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='pidstrength')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NfC")

##PK
## PK at -1SD
m0 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T)-sd(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T)-sd(pk,na.rm=T),scale=F)*pidstrength,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'
## PK at mean
m1 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*pidstrength,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## PK at +1
m2 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T)+sd(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T)+sd(pk,na.rm=T),scale=F)*pidstrength,data)
summary(m2)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_pk <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_pk$battery      <- c("Political\n knowledge")
forplot_pk$CRT <- factor(forplot_pk$CRT,levels = c("-1 SD","Mean","+1 SD"))

##COgnitive resources
## Cog resources -1SD
m0_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T)-sd(cogresources,na.rm=T),scale=F)*pidstrength+ OutParty*scale(cogresources,center=mean(cogresources,na.rm=T)-sd(cogresources,na.rm=T),scale=F)*pidstrength,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='pidstrength')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='pidstrength')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

##  Cog resources at mean
m1_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*pidstrength+OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*pidstrength,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='pidstrength')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='pidstrength')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## Cog resources at +1
m2_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T)+sd(cogresources,na.rm=T),scale=F)*pidstrength+ OutParty*scale(cogresources,center=mean(cogresources,na.rm=T)+sd(cogresources,na.rm=T),scale=F)*pidstrength,data)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='pidstrength')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='pidstrength')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_cog <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_cog$battery      <- c("Cognitive\n resources")

#Combine plots
forplot_comb <- rbind(forplot, forplot_nfc, forplot_pk, forplot_cog)
forplot_comb$CRT <- factor(forplot_comb$CRT,levels = c("-1 SD","Mean","+1 SD"))
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NfC","Political\n knowledge", "Cognitive\n resources"))

farm_main_no_covariates<-ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom")


